home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / TransSkel Pascal 2.5 / TransSkel / MultiSkel ƒ / MSkelHelp.p < prev    next >
Encoding:
Text File  |  1988-12-09  |  7.0 KB  |  259 lines  |  [TEXT/PJMM]

  1. {    TransSkel multiple-window demonstration: Help module}
  2.  
  3. {    This module handles a help window, in which text may be scrolled but}
  4. {    not edited.  A TextEdit record is used to hold the text, though.}
  5.  
  6. {    14 June 1986        Paul DuBois}
  7. {    7 January 1987    Ported to LightSpeed Pascal by Owen Hartnett            }
  8. {    Ωhm Software Company                                                        }
  9.  
  10. unit MSkelHelp;
  11.  
  12. interface
  13.  
  14.     uses
  15. {$IFC UNDEFINED THINK_PASCAL}
  16.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, 
  17. {$ENDC}
  18.         MultiSkelGlobals, common, TransSkel;
  19.  
  20.     procedure HelpWindInit;
  21.  
  22. implementation
  23.  
  24.     var
  25.         teHelp: TEHandle;        { handle to help window TextEdit record }
  26.         helpScroll: ControlHandle;    { help window scroll bar }
  27.         helpLine, halfPage: integer;    { line currently at top of window }
  28.                                     { number of lines in half a window }
  29.  
  30.     procedure Halt;
  31.  
  32.     begin
  33.         TEDispose(teHelp);
  34.         DisposeControl(helpScroll);
  35.         CloseWindow(helpWind);
  36.     end;
  37.  
  38. {    Scroll to the correct position.  lDelta is the}
  39. {    amount to CHANGE the current scroll setting by.}
  40.  
  41.     procedure DoScroll (lDelta: integer);
  42.  
  43.         var
  44.             newLine: integer;
  45.  
  46.     begin
  47.         newLine := helpLine + lDelta;
  48.         if newLine < 0 then
  49.             newLine := 0;
  50.         if newline > GetCtlMax(helpScroll) then
  51.             newline := GetCtlMax(helpScroll);
  52.         SetCtlValue(helpScroll, newLine);
  53.         lDelta := (helpLine - newLine) * (teHelp^^.lineHeight);
  54.         TEScroll(0, lDelta, teHelp);
  55.         helpLine := newLine;
  56.     end;
  57.  
  58. {    Filter proc for tracking mousedown in scroll bar.  The part code}
  59. {    of the part originally hit is stored as the control's reference}
  60. {    value.}
  61.  
  62.     procedure TrackScroll (theScroll: ControlHandle; partCode: integer);
  63.  
  64.         var
  65.             lDelta: integer;
  66.  
  67.     begin
  68.         if (partCode = GetCRefCon(theScroll)) then
  69.             begin
  70.                 case partCode of
  71.                     inUpButton: 
  72.                         lDelta := -1;
  73.                     inDownButton: 
  74.                         lDelta := 1;
  75.                     inPageUp: 
  76.                         lDelta := -halfPage;
  77.                     inPageDown: 
  78.                         lDelta := halfPage;
  79.                     otherwise
  80.                         ;
  81.                 end;
  82.                 DoScroll(lDelta);
  83.             end;
  84.     end;
  85.  
  86. {    Handle hits in scroll bar}
  87.  
  88.     procedure Mouse (thePt: Point; t: longint; mods: integer);
  89.  
  90.         var
  91.             thePart, ignore: integer;
  92.  
  93.     begin
  94.         thePart := TestControl(helpScroll, thePt);
  95.         if thePart = inThumb then
  96.             begin
  97.                 ignore := TrackControl(helpScroll, thePt, nil);
  98.                 DoScroll(GetCtlValue(helpScroll) - helpline);
  99.             end
  100.         else if thePart <> 0 then
  101.             begin
  102.                 SetCRefCon(helpScroll, longint(thePart));
  103.                 ignore := TrackControl(helpScroll, thePt, @TrackScroll);
  104.             end;
  105.     end;
  106.  
  107. {    Update help window.  The update event might be in response to a}
  108. {    window resizing.  If so, resize the rects and recalc the linestarts}
  109. {    of the text.  To resize the rects, only the right edge of the}
  110. {    destRect need be changed (the bottom is not used, and the left and}
  111. {    top should not be changed). The viewRect should be sized to the}
  112. {    screen.  Pull text down if necessary to fill window.}
  113.  
  114.     procedure update (resized: Boolean);
  115.  
  116.         var
  117.             r: Rect;
  118.             visLines, lHeight, topLines, nLines, scrollLines, ignore: integer;
  119.  
  120.     begin
  121.         r := helpWind^.portRect;
  122.         EraseRect(r);
  123.         if resized then
  124.             begin
  125.                 r.left := r.left + 4;
  126.                 r.bottom := r.bottom - 2;
  127.                 r.top := r.top + 2;
  128.                 r.right := r.right - 19;
  129.  
  130.                 teHelp^^.destRect.right := r.right;
  131.                 teHelp^^.viewRect := r;
  132.                 TECalText(teHelp);
  133.                 lHeight := teHelp^^.lineHeight;
  134.                 nLines := teHelp^^.nLines;
  135.                 visLines := (r.bottom - r.top) div lHeight;
  136.                 halfPage := visLines div 2;
  137.                 topLines := (r.top - teHelp^^.destRect.top) div lHeight;
  138.                 scrollLines := visLines - (nLines - topLines);
  139.                 if (scrollLines > 0) and (topLines > 0) then
  140.                     begin
  141.                         if scrollLines > topLines then
  142.                             scrollLInes := topLines;
  143.                         TEScroll(0, scrollLines * lHeight, teHelp);
  144.                     end;
  145.                 scrollLines := nLines - visLines;
  146.                 helpLine := (r.top - teHelp^^.destRect.top) div lHeight;
  147.  
  148. {    move and resize the scroll bar as well.  The ValidRect call is done}
  149. {    because the HideControl adds the control bounds box to the update}
  150. {    region - which would generate another update event!  Since everything}
  151. {    gets redrawn below, the ValidRect is used to cancel the update.}
  152.  
  153.                 HideControl(helpScroll);
  154.                 r := helpWind^.portRect;
  155.                 r.left := r.right - 15;
  156.                 r.bottom := r.bottom - 14;
  157.                 r.top := r.top - 1;
  158.                 r.right := r.right + 1;
  159.                 SizeControl(helpScroll, r.right - r.left, r.bottom - r.top);
  160.                 MoveControl(helpScroll, r.left, r.top);
  161.                 if nLines - visLines < 0 then
  162.                     ignore := 0
  163.                 else
  164.                     ignore := nLines - vislines;
  165.                 SetCtlMax(helpScroll, ignore);
  166.                 SetCtlValue(helpScroll, helpLine);
  167.                 ShowControl(helpScroll);
  168.             end;
  169.         DrawGrowBox(helpWind);
  170.         DrawControls(helpWind);    { redraw scroll bar }
  171.         r := teHelp^^.viewRect;
  172.         TEUpdate(r, teHelp);        { redraw text display }
  173.         ValidRect(helpWind^.portRect);
  174.     end;
  175.  
  176. {    When the window comes active, disable the Edit menu and highlight}
  177. {    the scroll bar if there are any lines not visible in the content}
  178. {    region.  When the window is deactivated, enable the Edit menu and}
  179. {    un-highlight the scroll bar.}
  180.  
  181.     procedure Activate (active: Boolean);
  182.  
  183.         var
  184.             ignore: integer;
  185.  
  186.     begin
  187.         DrawGrowBox(helpWind);
  188.         if active then
  189.             begin
  190.                 DisableItem(editMenu, 0);
  191.                 if GetCtlMax(helpScroll) > 0 then
  192.                     ignore := 0
  193.                 else
  194.                     ignore := 255;
  195.                 HiLiteControl(helpScroll, ignore);
  196.             end
  197.         else
  198.             begin
  199.                 EnableItem(editMenu, 0);
  200.                 HiLiteControl(helpScroll, 255);
  201.             end;
  202.         DrawMenuBar;
  203.     end;
  204.  
  205.     procedure HelpWindInit;
  206.  
  207.         var
  208.             r: Rect;
  209.             textHandle: Handle;
  210.             visLines, scrollLines: integer;
  211.  
  212.     begin
  213.         helpWind := GetNewWindow(helpWindRes, nil, WindowPtr(-1));
  214.         dummy := SkelWindow(helpWind, @Mouse, nil, @Update, @Activate, nil, @Halt, nil, true);
  215.         TextFont(0);
  216.         TextSize(0);
  217.  
  218.         r := helpWind^.portRect;
  219.         r.left := r.left + 4;
  220.         r.bottom := r.bottom - 2;
  221.         r.top := r.top + 2;
  222.         r.right := r.right - 19;
  223.         teHelp := TENew(r, r);
  224.         textHandle := GetResource('TEXT', helpTextRes);        {read help text}
  225.         HLock(textHandle);
  226.         TEInsert(textHandle^, GetHandleSize(textHandle), teHelp);
  227.         HUnlock(textHandle);
  228.         ReleaseResource(textHandle);    { done with it, so goodbye }
  229.  
  230. {    Now figure out how many lines will fit in the window and how many}
  231. {    will not.  Determine the number of lines in half a window for use}
  232. {    in tracking clicks in the page up and page down regions of the}
  233. {    scroll bar.  Then create the scroll bar .  Make sure the borders }
  234. {    overlap the window frame and the frame of the grow box. }
  235.  
  236.         visLines := (r.bottom - r.top) div teHelp^^.lineHeight;
  237.         scrollLines := teHelp^^.nLines - visLines;
  238.         halfPage := visLines div 2;
  239.         helpline := 0;
  240.         r := helpWind^.portRect;
  241.         r.left := r.right - 15;
  242.         r.bottom := r.bottom - 14;
  243.         r.top := r.top - 1;
  244.         r.right := r.right + 1;
  245.  
  246. {    Build the scroll bar.  Don't need to bother testing whether to}
  247. {    highlight it or not, since that will be done in response to the}
  248. {    activate event.}
  249.  
  250.         helpScroll := NewControl(helpWind, r, '', true, helpLine, 0, scrollLines, scrollBarProc, 0);
  251.  
  252. {    GetNewWindow generates an update event for entire portRect.}
  253. {    Cancel it, since the everything has been drawn already,}
  254. {    except for the grow box (which will be drawn in response}
  255. {    to the activate event).}
  256.  
  257.         ValidRect(helpWind^.portRect);
  258.     end;
  259. end.